import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;

import edu.gatech.coc.betameisters.infoholders.Appointment;
import edu.gatech.coc.betameisters.infoholders.DoctorsOrders;
import edu.gatech.coc.betameisters.infoholders.LabWorkReport;
import edu.gatech.coc.betameisters.infoholders.MedicalHistory;
import edu.gatech.coc.betameisters.infoholders.Prescription;
import edu.gatech.coc.betameisters.infoholders.Treatment;
import edu.gatech.coc.betameisters.infoholders.Constants.VisitType;
import edu.gatech.coc.betameisters.infoholders.MedicalHistory.Gender;
import edu.gatech.coc.betameisters.people.Doctor;
import edu.gatech.coc.betameisters.people.Nurse;
import edu.gatech.coc.betameisters.people.Patient;
import edu.gatech.coc.betameisters.people.Person;
import edu.gatech.coc.betameisters.people.SystemAdmin;
import edu.gatech.coc.betameisters.utilities.Database;

public class DatabaseTester {
	public static void main(String[] args) {
		Database.deleteByUsername("NewID");
		// Test for creating a new patient
		Patient p = new Patient("first","last");
		p.setUserPass("newPassword");
		p.setUserID("NewID");
		
		System.out.println("These two results should be identical ");
		System.out.println(p + "A------------- UserID: " + p.getUserID() + " Password: " + p.getUserPass());
		Patient p1 = Database.loadPatient("NewID");
		System.out.println(p1 + "B------------- UserID: " + p1.getUserID() + " Password: " + p1.getUserPass());
		System.out.println();
		
		SystemAdmin admin1 = new SystemAdmin("This", "Admin");		
		
		Nurse n = new Nurse("firstNurse", "lastNurse");
		n.setUserPass("newPassword");
		n.setUserID("NewNurseID");
		n.setCurrentPatient(p1);
		System.out.println("These two results should be identical");
		System.out.println(n + " UserID: " + n.getUserID() + " Password: " + n.getUserPass());
		Nurse n1 = Database.loadNurse(n.getUserID());
		System.out.println(n1 + " UserID: " + n1.getUserID() + " Password: " + n1.getUserPass());
		System.out.println("Nurse's patient: "+n1.getCurrentPatient());
		System.out.println();
		
		Doctor d = new Doctor("firstDoctor", "lastDoctor");
		d.setUserPass("newPassword");
		d.setUserID("NewDoctorID");
		d.setCurrentPatient(p1);
		System.out.println("These two results should be identical");
		System.out.println(d + " UserID: " + d.getUserID() + " Password: " + d.getUserPass());
		Doctor d1 = Database.loadDoctor(d.getUserID());
		System.out.println(d1 + " UserID: " + d1.getUserID() + " Password: " + d1.getUserPass());
		System.out.println("Doctor's patient: "+d1.getCurrentPatient());
		System.out.println();
		
		// Medical History Test
		Patient medPat = new Patient("Unique", "Name");
		medPat.setUserID("UniqueUID");
		System.out.println("New Patient's username: "+medPat.getUserID());
		MedicalHistory medHist = new MedicalHistory("123 Fake Street", "555-555-5555", 
				Gender.MALE, "1/1/1900", "InsureCo");
		MedicalHistory medHist1 = new MedicalHistory("567 Diff Street", "444-444-4444", 
				Gender.FEMALE, "2/2/1922", "HealthCross");
		medPat.updateMedHist(medHist);
		System.out.println(medHist);
		Patient medPat1 = Database.loadPatient(medPat.getUserID());
		System.out.println(medPat1.getMedHist());
		
		medPat1.updateMedHist(medHist1);
		System.out.println(medPat1.getMedHist());
		
		// Authenticate Test
		Person testAuth = Database.authenticateUser(n.getUserID(), n.getUserPass());
		if(testAuth != null) System.out.println("User authenticated: "+testAuth);
		
		// Doctors Orders Test
		Patient docOrdPat = new Patient("IHave","DOrders");
		docOrdPat.setUserID("DocOrdTest");
		DoctorsOrders ords = new DoctorsOrders("Name1", "Name2", "Take some medicine");
		docOrdPat.setMyDocOrders(ords, d1.getUserID());
		System.out.println(docOrdPat.getMyDocOrders());
		Patient docOrdPatLoad = Database.loadPatient(docOrdPat.getUserID());
		System.out.println(docOrdPat + " "+ docOrdPat.getUserID());
		System.out.println(docOrdPatLoad + " "+ docOrdPatLoad.getUserID());
		System.out.println("new Orders:" +docOrdPatLoad.getMyDocOrders());
				
		// Appointment Test
		System.out.println();
		GregorianCalendar start = new GregorianCalendar(2011, 5, 25, 12, 0, 0);
		GregorianCalendar end = new GregorianCalendar(2011, 5, 26, 12, 0, 0);
		Appointment app = new Appointment(docOrdPat, n, start, end, "This is an appointment");
		Appointment app1 = new Appointment(docOrdPat, d, start, end, "This is an appointment");
		List<Appointment> apps = Database.loadAppointment(docOrdPat.getUserID());
		System.out.println("Printing appointments...");
		for(Appointment a : apps) {
			System.out.println(a);
		}
		
		System.out.println();
		System.out.println("SEARCH TEST");
		// search test
		List<Person> search= Database.searchUsersByName("Unique", "Name");
		for(Person per : search) {
			System.out.println("Found "+per.getUserID());
		}
		
		System.out.println();
		System.out.println("BREAK LINE ----------------------------------------");
		System.out.println();
		System.out.println("Testing nurse's create appointment");
		Nurse appNurse = new Nurse("Create", "Appoint");
		appNurse.setCurrentPatient(p);
		appNurse.createAppointment(0, 11, 2012, 11, 11, 12, 12, "Nurse appoint1");
		appNurse.createAppointment(0, 11, 2014, 11, 11, 12, 12, "Nurse appoint2");
		appNurse.createAppointment(0, 11, 2013, 11, 11, 12, 12, "Nurse appoint3");
		for(Appointment a : Database.loadAppointment(appNurse.getUserID())) {
			System.out.println(a);
		}
		
		Patient John = new Patient("John", "Jackson");
		Patient Jack = new Patient("Jack", "Johnson");
		Patient Jill = new Patient("Jill", "Smith");
		MedicalHistory johnMedHist = new MedicalHistory("800 Fowler St", "5555555555",
				Gender.MALE, "06/19/1991", "United Healthcare");
		MedicalHistory jackMedHist = new MedicalHistory("206 Battery way", "7706348293",
				Gender.MALE, "08/19/1992", "United Healthcare");
		MedicalHistory jillMedHist = new MedicalHistory("800 Fowler St.", "5559902234",
				Gender.FEMALE, "02/29/1989", "Amway Healthcare");
		LinkedList<Treatment> jillTreatment = new LinkedList<Treatment>();
		jillTreatment.add(new Treatment(VisitType.OFFICEVISIT));
		Treatment withDetails = new Treatment(VisitType.PHONECONSULT);
		withDetails.setTreatmentDetails("Talked with her over the phone.");
		jillTreatment.add(withDetails);
		DoctorsOrders myOrder = new DoctorsOrders("Doctor1", "John", "Get lots of rest");
		Doctor Jim = new Doctor("Jim", "Jimly");
		Prescription myPrescription = new Prescription(Jim, John);
		myPrescription.setDrugDetails("Strong antibiotic");
		myPrescription.setDrugInstructions("Take one every 12 hours");
		myPrescription.setDrugName("Revivitol");
		myPrescription.setDrugPrice("$1/pill");
		myOrder.setPrescriptionOrder(myPrescription);
		myOrder.setLabReport(new LabWorkReport("analysis lab","Positive","Minor treatment necessary"));
		John.setMyDocOrders(myOrder, "Jim Jimly");
		
		Nurse Sam = new Nurse("Sam", "Smith");
		new Appointment(John, Sam, new GregorianCalendar(2011,4,22,3,30,00), 
				new GregorianCalendar(2011,4,22,4,30,00), "Just a checkup");
		new Appointment(Jill, Sam, new GregorianCalendar(2011,4,23,3,30,00), 
				new GregorianCalendar(2011,4,23,4,30,00), "Possible infection");
		
		
		new Appointment(John, Jim, new GregorianCalendar(2011,4,12,3,30,00), 
				new GregorianCalendar(2011,4,22,4,30,00), "Annual Physical");
		new Appointment(Jack, Jim, new GregorianCalendar(2011,4,13,3,30,00), 
				new GregorianCalendar(2011,4,23,4,30,00), "Broken finger");
		
		SystemAdmin Kurt = new SystemAdmin("Kurt", "Nelson");
		
	}
}
